<template>
  <view>
    <view class="goods-item">
      <view class="goods-item-left">
        <radio :checked="goods.goods_state" @click="showRadioHandler" color="#C00000" v-if="showRadio"></radio>
        <image :src="goods.goods_small_logo || defaultPic"></image>
      </view>
      <view class="goods-item-right">
        <text class="goods-name">{{goods.goods_name}}</text>
        <view class="goods-box-bot">
          <text class="goods-price">￥{{goods.goods_price|numFixed}}</text>
          <uni-number-box :min="1" v-if="showNumber" @change="numberChange" :value="goods.goods_count"></uni-number-box>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    props:{
      goods:{
        type:Object,
        default:{}
      },
      showRadio:{
        type:Boolean,
        default:false
      },
      showNumber:{
        type:Boolean,
        default:false
      }
    },
    name:"goods",
    data() {
      return {
        // 默认的空图片
        defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
      };
    },
    filters:{
      numFixed(num){
       return Number(num).toFixed(2)
      }
    },
    methods:{
      showRadioHandler(){
        this.$emit('radio-change',{
          goods_id:this.goods.goods_id,
          goods_state:!this.goods.goods_state
        })
      },
      numberChange(val){
        //校验数值合法
        let num = parseInt(val)
        if(!num){
          val = 1
        }
        this.$emit('number-change',{
          goods_id:this.goods.goods_id,
          goods_count:+val
        })
      }
    }
  }
</script>

<style lang="scss">
.goods-item{
    display: flex;
    padding:10px 5px;
    border-bottom:1px solid #efefef;
    .goods-item-left{
      display: flex;
      align-items: center;
      image{
        width:100px;
        height:100px;
        margin-right: 5px;
      }
    }
    .goods-item-right{
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      .goods-name{
        font-size: 13px;
      }
      .goods-box-bot{
        display: flex;
        justify-content:space-between;
        .goods-price{
          font-size: 16px;
          color:#C00000;
        }
      }
      
    }
  }

</style>
